package com.zenithmind.library.strategy.impl;

import com.zenithmind.common.result.Result;
import com.zenithmind.library.enums.BookStatus;
import com.zenithmind.library.strategy.BookStatusStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 可借阅状态处理策略
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Slf4j
@Component
public class AvailableBookStatusStrategy implements BookStatusStrategy {

    @Override
    public BookStatus getSupportedStatus() {
        return BookStatus.AVAILABLE;
    }

    @Override
    public Result<Void> handleStatusChange(String bookCopyId, BookStatus fromStatus, BookStatus toStatus) {
        log.info("处理图书副本状态变更，副本ID：{}，从 {} 变更为 {}", bookCopyId, fromStatus, toStatus);
        
        if (!isValidStatusChange(fromStatus, toStatus)) {
            return Result.fail("无效的状态变更");
        }
        
        switch (toStatus) {
            case BORROWED:
                return handleBorrow(bookCopyId);
            case RESERVED:
                return handleReserve(bookCopyId);
            case MAINTENANCE:
                return handleMaintenance(bookCopyId);
            case DAMAGED:
                return handleDamage(bookCopyId);
            case LOST:
                return handleLost(bookCopyId);
            case WITHDRAWN:
                return handleWithdraw(bookCopyId);
            default:
                return Result.fail("不支持的状态变更");
        }
    }

    @Override
    public boolean isValidStatusChange(BookStatus fromStatus, BookStatus toStatus) {
        // 可借阅状态可以变更为任何其他状态
        return fromStatus == BookStatus.AVAILABLE && toStatus != BookStatus.AVAILABLE;
    }

    @Override
    public String getStatusDescription() {
        return "图书可借阅，可以被用户借阅或预约";
    }

    private Result<Void> handleBorrow(String bookCopyId) {
        log.info("处理图书借阅，副本ID：{}", bookCopyId);
        // 实现借阅逻辑
        return Result.success();
    }

    private Result<Void> handleReserve(String bookCopyId) {
        log.info("处理图书预约，副本ID：{}", bookCopyId);
        // 实现预约逻辑
        return Result.success();
    }

    private Result<Void> handleMaintenance(String bookCopyId) {
        log.info("处理图书维护，副本ID：{}", bookCopyId);
        // 实现维护逻辑
        return Result.success();
    }

    private Result<Void> handleDamage(String bookCopyId) {
        log.info("处理图书损坏，副本ID：{}", bookCopyId);
        // 实现损坏处理逻辑
        return Result.success();
    }

    private Result<Void> handleLost(String bookCopyId) {
        log.info("处理图书丢失，副本ID：{}", bookCopyId);
        // 实现丢失处理逻辑
        return Result.success();
    }

    private Result<Void> handleWithdraw(String bookCopyId) {
        log.info("处理图书下架，副本ID：{}", bookCopyId);
        // 实现下架逻辑
        return Result.success();
    }
}
